package com.ruoyi.framework.extendsions.config;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.extendsions.entity.AppEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cj
 */
@Component
public class WechatAppConfig {
    private static Map<String, AppEntity> AppMap;

    private static String keys;

    private static String appid;

    private static String secret;

    private static String deptId;

    private static String roleId;

    private static String envVersion;

    private static void init() {
        if (StringUtils.isEmpty(WechatAppConfig.keys)) return;
        String[] keysList = WechatAppConfig.keys.split(",");
        if (keysList.length < 1) return;

        String[] appidList = WechatAppConfig.appid.split(",");
        String[] secretList = WechatAppConfig.secret.split(",");
        String[] deptIdList = WechatAppConfig.deptId.split(",");
        String[] roleIdList = WechatAppConfig.roleId.split(",");
        for(int i=0; i < keysList.length; i++) {
            String key = keysList[i];
            AppEntity task = new AppEntity();
            task.setAppkey(key);
            task.setAppid(appidList[i]);
            task.setSecret(secretList[i]);
            task.setDeptId(Long.parseLong(deptIdList[i]));
            task.setRoleId(new Long[]{Long.parseLong(roleIdList[i])});
            AppMap.put(key, task);
        }
    }


    /**
     * 获取appid 和 secret
     *
     * @param code
     * @return
     */
    public static AppEntity getApp(String code) {

        if (StringUtils.isNull(AppMap)) {
            AppMap = new HashMap<>();
            init();
        }
        AppEntity app = AppMap.get(code);
        if (StringUtils.isNull(app)) {
            throw new ServiceException("无效的app:" + code);
        }
        return app;
    }
    /**
     * 获取appid 和 secret
     *
     *
     * @return
     */
//    public static AppEntity getApp() {
//        return getApp(ToolsUtils.getAppid());
//    }
    public static AppEntity getApp() {
        return getApp("gemini");
    }
    public static String getKeys() {
        return keys;
    }

    @Value("${weixin.keys}")
    public void setKeys(String keys) {
        WechatAppConfig.keys = keys;
    }

    public static String getAppid() {
        return appid;
    }

    @Value("${weixin.appid}")
    public void setAppid(String appid) {
        WechatAppConfig.appid = appid;
    }

    public static String getSecret() {
        return secret;
    }

    @Value("${weixin.secret}")
    public void setSecret(String secret) {
        WechatAppConfig.secret = secret;
    }

    public static String getDeptId() {
        return deptId;
    }

    @Value("${weixin.deptId}")
    public void setDeptId(String deptId) {
        WechatAppConfig.deptId = deptId;
    }

    public static String getRoleId() {
        return roleId;
    }

    @Value("${weixin.roleId}")
    public void setRoleId(String roleId) {
        WechatAppConfig.roleId = roleId;
    }

    public static String getEnvVersion() {
        return envVersion;
    }

    @Value("${weixin.env_version}")
    public void setEnvVersion(String envVersion) {
        WechatAppConfig.envVersion = envVersion;
    }
}
